import logging
import time
import os
from wsgiref import simple_server
log = logging.getLogger(__name__)

def serve_rss(environ, start_response):
    f = open(os.path.join(os.path.dirname(__file__), 'pypi.rss'), 'r')
    f.seek(0,2)
    totalbytes = f.tell()
    f.seek(0)
    start_response('200 OK', [
        ('Content-Type', 'application/xml'),
        ('Content-Length', str(totalbytes))])
        
    chunksize = 1024
    while 1:
        data = f.read(chunksize)
        if not data:
            break
        yield data
    
def main():
    logging.basicConfig(level=logging.DEBUG,
                        format='[%(asctime)s] %(message)s')
    port = 9000
    log.info("starting stub PyPi RSS server on port %s", port)
    httpd = simple_server.WSGIServer(('',port), simple_server.WSGIRequestHandler)
    httpd.set_app(serve_rss)
    httpd.serve_forever()

if __name__ == '__main__':
    main()